package com.huk.chengka.ckf.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.huk.chengka.ckf.entity.CkResponse;
import com.huk.chengka.ckf.entity.ProductInfo;
import com.huk.chengka.ckf.service.FindCardService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author huk
 * @date 2024/1/17 16:14
 * @Description $
 */
@Service
@Slf4j
public class FindCardServiceImpl implements FindCardService {

    @Value("${ck.iv:12345678}")
    @Getter
    private String iv;

    @Value("${ck.key:qAfmGitJ}")
    @Getter
    private String key;

    @Value("${ck.query-product-info:https://wechat.chengquan.cn/ccb/queryProductInfo?cardPwd=}")
    @Getter
    private String queryPath;

    @Value("${ck.order-detail:https://wechat.chengquan.cn/ccb/orderDetail?cardPwd=}")
    @Getter
    private String detailPath;


    private static Logger ckLog = LoggerFactory.getLogger("ckFind");


    @Override
    public List<ProductInfo> find(List<String> cardList) {
        log.info("开始查询{}个卡号",cardList.size());
        Assert.notEmpty(cardList);
        List<ProductInfo> cardInfos = new ArrayList<>();
        for (String card : cardList) {
            CkResponse ckResponse = query(card);
            if (available(ckResponse)) {
                cardInfos.add(getProductInfo(ckResponse));
            }
            ThreadUtil.safeSleep(3000);
        }
        log.info("本次查询{}个，存在{}个",cardList.size(),cardInfos.size());
        return cardInfos;
    }


    @Override
    public CkResponse query(String card) {
        DES des = new DES(Mode.CBC, Padding.PKCS5Padding, StrUtil.utf8Bytes(key), StrUtil.utf8Bytes(iv));
        String encrypted = des.encryptBase64(card);
        String url = StrBuilder.create(queryPath).append(encrypted).toString();
        HttpRequest httpRequest = HttpUtil.createPost(url)
                .header(Header.HOST, "wechat.chengquan.cn")
                .contentType("application/x-www-form-urlencoded; charset=UTF-8");
        String body = httpRequest.execute().body();
        CkResponse ckResponse = JSON.parseObject(body, CkResponse.class);
       if(available(ckResponse)){
           log.info("卡号【{}】，存在相应产品信息",card);
           ckLog.info("卡号【{}】,返回信息{}",card,JSON.toJSONString(ckResponse));
       }
       return ckResponse;
    }

    @Override
    public boolean available(CkResponse ckResponse) {
        Integer errorCode = ckResponse.getErrorCode();
        Assert.notEquals(CkResponse.PARAM_ERROR_1,errorCode);
        if (CollUtil.contains(CkResponse.invalidCodeList(), errorCode)) {
            return false;
        }else if (CkResponse.OK_CODE_1.equals(errorCode)) {
            return true;
        } else {
            return true;
        }
    }

    @Override
    public ProductInfo getProductInfo(CkResponse ckResponse) {
        Assert.notNull(ckResponse);
        String errorMsg = ckResponse.getErrorMsg();
        Assert.notEmpty(errorMsg);
        return JSON.parseObject(errorMsg, ProductInfo.class);
    }

}
